<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Aslider extends Controller_Acommon {

	public $template = 'alayout/amain';

    public function params() {
        return array(
            'waymark' => 'title',
            'fields' => array(
                'title' => array(
                    'title' => 'Заголовок',
                    'type' => 'text',
                    'name' => 'title',
                ),
                'price' => array(
                    'title' => 'Цена',
                    'type' => 'text',
                    'name' => 'price',
                ),
                'link' => array(
                    'title' => 'Ссылка',
                    'type' => 'text',
                    'name' => 'link',
                ),
                'image' => array(
                    'title' => 'Изображение',
                    'type' => 'image',
                    'name' => 'image',
                    'image_params' => array(
                        'width' => '751',
                        'height' => '300',
                        'dir' => $_SERVER['DOCUMENT_ROOT'].'/static/uploads/slider/',
                        'url' => URL::base().'static/uploads/slider/',
                    )
                ),
                'type' => array(
                    'title' => 'Тип',
                    'type' => 'select',
                    'name' => 'type',
                    'select_params' => array(
                        'values' => array('без текста', 'с текстом'),
                    )
                ),
            ),
        );
    }

	public function action_index() {
        $params = $this->params();
		$this->showDefaultList(); // Рендерим список данных и форму редактирования\добавления
        $this->showPagination(); // Пагинация
        $this->showDefaultForm($params, ORM::factory($this->request->controller()));
	}

    public function action_add() {
        $newObj = ORM::factory($this->request->controller());
        $newObj->title = $this->request->post('title');
        $newObj->price = $this->request->post('price');
        $newObj->link = $this->request->post('link');
        $newObj->image = $this->uploadImage('image', NULL);
        $newObj->type = $this->request->post('type');
        $newObj->save();
        $this->request->redirect('administration/'.$this->request->controller().'/'.$this->request->param('page'));
    }

    public function action_edit() {
        $obj = ORM::factory($this->request->controller(), $this->request->param('id')); // Получаем редактируемую запись

        if ($this->request->param('save')) {
            $obj->title = $this->request->post('title');
            $obj->price = $this->request->post('price');
            $obj->link = $this->request->post('link');
            $obj->image = $this->uploadImage('image', $obj->image);
            $obj->type = $this->request->post('type');
            $obj->save();
            $this->request->redirect('administration/'.$this->request->controller().'/'.$this->request->param('page').'/edit/'.$this->request->param('id'));
            exit();
        }

        $params = $this->params();
        $this->showDefaultList(); // Рендерим список данных и форму редактирования\добавления
        $this->showPagination(); // Пагинация
        $this->showDefaultForm($params, $obj, 'edit');
    }

    public function action_delete() {
        $obj = ORM::factory($this->request->controller(), $this->request->param('id'));

        if (!empty($obj->image)) { // Удаляем изображение если оно есть
            unlink(arr::path($this->params(), 'image.image_params.url').'/'.$obj->image);
        }

        $obj->delete();
        $this->request->redirect('administration/'.$this->request->controller().'/'.$this->request->param('page').'/edit/'.$this->request->param('id'));
    }
}